package com.shengqiang.cloud.ups.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.common.model.PageResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsPermission;
import com.shengqiang.cloud.ups.dao.modal.UpmsRole;
import com.shengqiang.cloud.ups.dao.modal.UpmsUser;
import com.shengqiang.cloud.ups.dao.modal.UpmsUserPermission;
import com.shengqiang.cloud.ups.service.UpsUserWebService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/23.
 * 玩家信息管理
 */

@Controller
@RequestMapping(value = "user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UpsUserWebService userWebService;

    @RequestMapping(value = "showUserList")
    public String showUserList(){
        return "/user/userList";
    }

    @RequiresPermissions(value = "upms:users:users:list")
    @ResponseBody
    @RequestMapping(value = "userList",method = {RequestMethod.POST,RequestMethod.GET})
    public PageResult<UpmsUser> selectUserPage(HttpServletRequest request){
        try {
            int limit = Integer.parseInt(request.getParameter("limit"));
            int page = Integer.parseInt(request.getParameter("page"));
            int locked=Integer.parseInt(request.getParameter("locked"));
            String keyWord=request.getParameter("keyWord");
            PageResult<UpmsUser> pageResult=userWebService.selectUserPage(page,limit,locked,keyWord);
            return pageResult;
        }catch (Exception ex){
            logger.error("解析参数异常:{}",ex.getMessage(),ex);
            return new PageResult<>(1,"fail",0,null);
        }
    }

    @RequiresPermissions(value = "upms:users:user:audit")
    @ResponseBody
    @RequestMapping(value = "audit",method = RequestMethod.POST)
    public JsonResult auditUser(HttpServletRequest request){
        try{
            String userIds=request.getParameter("userIds");
            int state=Integer.parseInt(request.getParameter("locked"));
            JsonResult result=userWebService.batchAuditUser(userIds,state);
            return result;
        }catch (Exception ex){
            logger.error("解析审核用户参数出现异常:{}",ex.getMessage());
            return new JsonResult(0,"参数解析异常");
        }
    }

    @RequiresPermissions(value = "upms:users:users:edit")
    @ResponseBody
    @RequestMapping(value = "updateUser",method = {RequestMethod.GET,RequestMethod.POST})
    public JsonResult updateUser(@RequestParam Map<String,Object> params){
        return userWebService.updateUser(params);
    }

    @RequiresPermissions(value = "upms:users:users:assginRoles")
    @ResponseBody
    @RequestMapping(value = "saveUserRole",method = {RequestMethod.POST,RequestMethod.GET})
    public JsonResult saveUserRole(@RequestBody Map<String,Object> params){
        return userWebService.saveUserRole(params);
    }

    @ResponseBody
    @RequestMapping(value = "/selectUserRoleById/{userId}",method = RequestMethod.GET)
    public JsonResult<List<UpmsRole>> selectUserRolesByUserId(@PathVariable int userId){
        return userWebService.selectUserRoles(userId);
    }

    @RequiresPermissions(value = "upms:users:users:assginPermissions")
    @ResponseBody
    @RequestMapping(value = "savePriPermission",method = {RequestMethod.POST,RequestMethod.GET})
    public JsonResult savePrivatePermissions(@RequestBody Map<String,Object> params){
        return userWebService.savePrivatePermissions(params);
    }

    @ResponseBody
    @RequestMapping(value = "/selectPriPermissions/{userId}",method = RequestMethod.GET)
    public JsonResult<List<UpmsPermission>> selectPriPermissions(@PathVariable int userId){
        return userWebService.selectUserPermissions(userId);
    }
}
